package com.REPDPMSystem.Service.Handler;

import com.REPDPMSystem.Service.EmployeeService;
import com.REPDPMSystem.Entity.Employee;
import com.REPDPMSystem.Entity.EmployeePosition;
import com.REPDPMSystem.Mapper.EmployeeMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
public class EmployeeHandler extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService {
    @Override
    public  List<Employee> getEmployeeInfo(String userName,String passWord){
        QueryWrapper<Employee> queryWrapper = Wrappers.query();
        queryWrapper.eq("user_name", userName)
                    .eq("password",passWord);
        return baseMapper.selectList(queryWrapper);
    }
    @Override
    public  List<Employee> getEmployeeInfo(){
        return baseMapper.selectList(null);
    }
    @Override
    public boolean changeEmployeeInfo(Employee employee){
        UpdateWrapper<Employee> updateWrapper = Wrappers.update();
        updateWrapper.eq("staff_id",employee.getStaff_id());
        if(baseMapper.update(employee,updateWrapper)>0) return true;
        return false;
    }
    @Override
    public  List<Employee> getEmployeeInfoByName(String name){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.like("employee_name",name);
        return baseMapper.selectList(wrapper);

    }

    @Override
    public List<Employee> getDepartmentEmployee(int employee_department) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("employee_department",employee_department);

        return baseMapper.selectList(wrapper);
    }

    @Override
    public  EmployeePosition getEmployeePositionById(String staffId) {
        QueryWrapper<Employee> queryWrapper = Wrappers.query();
        queryWrapper.eq("staff_id",staffId);
        switch(baseMapper.selectOne(queryWrapper).getEmployee_status()){
            case 0:return EmployeePosition.Boss;
            case 1:return EmployeePosition.Model_Supervisor;
            case 2:return EmployeePosition.Rendering_Supervisor;
            case 3:return EmployeePosition.Post_production_Supervisor;
            case 4:return EmployeePosition.Model_Employee;
            case 5:return EmployeePosition.Rendering_Employee;
            case 6:return EmployeePosition.Post_production_Employee;
            case 7:return EmployeePosition.Apprentice;
        }
        return null;
    }

    @Override
    public int getEmployeeDepartment(String employeeId) {
        return baseMapper.selectOne(
                new QueryWrapper<Employee>()
                .eq("employee_id",employeeId))
                .getEmployee_department();
    }

    @Override
    public boolean createEmployee(Employee employee){
        employee.setStaff_id(UUID.randomUUID().toString());
        return baseMapper.insert(employee)>0;
    }

    @Override
    public boolean changePassword(String userName, String passWord, String newPassword) {
        Employee t = baseMapper.selectOne(new QueryWrapper<Employee>()
                .eq("user_name", userName).eq("password", passWord));
        if(t==null)return false;
        t.setPassword(newPassword);
        baseMapper.updateById(t);
        return true;
    }

    @Override
    public String getEmployeeNameById(String employeeId) {
        if(Objects.equals(employeeId, "000000000000000000000000000000000000")) return "null";
        return baseMapper.selectOne(new QueryWrapper<Employee>()
                .eq("staff_id",employeeId)).getEmployee_name();
    }
}
